package net.jalbum.filters;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.jalbum.filters.components.JInstantTextField;
import se.datadosen.component.JColorSelector;
import se.datadosen.jalbum.Msg;

public class StraightenControl extends FilterControl{
	/**
	 *
	 */
	private static final long serialVersionUID = -8762814379216308341L;
	protected StraightenFilterPlugin straightenFilterPlugin;
	protected JTextField tfAngle;
	protected JCheckBox random;
	protected JCheckBox showGrid;
	protected JComboBox resizeOption;
	protected Color color;
	protected JColorSelector bgcolor;
	protected double angle;

	public StraightenControl(){}

	public StraightenControl(StraightenFilterPlugin straightenFilterPlugin) {

		init(straightenFilterPlugin);

		addstraightenControls();
	}

	protected void init(StraightenFilterPlugin straightenFilterPlugin) {
		this.straightenFilterPlugin = straightenFilterPlugin;

		super.init(straightenFilterPlugin);
	}

	public void addstraightenControls(){
		//Create UI-elements
		tfAngle = new JInstantTextField(Integer.toString(0),4);
		//random = new JCheckBox(texts.getString("ui.straighten.random"));
		showGrid = new JCheckBox(Msg.getString(this,"filters.straighten.showGrid"));
		showGrid.setSelected(true);
		bgcolor = new JColorSelector(Msg.getString(this,"filters.straighten.bgColor"));
		bgcolor.setColor(Color.WHITE);

		resizeOption = new JComboBox();
		resizeOption.addItem(new String(Msg.getString(this,"filters.straighten.resizeCrop")));
		resizeOption.addItem(new String(Msg.getString(this,"filters.straighten.resizeCropAndZoom")));
		resizeOption.addItem(new String(Msg.getString(this,"filters.straighten.resizeEnlarge")));

		tfAngle.addActionListener(this);
		//random.addActionListener(this);

		showGrid.addActionListener(this);
		resizeOption.addActionListener(this);
		bgcolor.addActionListener(this);

		this.add("p",new JLabel(Msg.getString(this,"filters.straighten.straighten")));
		this.add(tfAngle);
		this.add(new JLabel("<html>&deg;</html>"));
		//this.add("br",random);
		this.add("br",bgcolor);
		this.add(new JLabel(Msg.getString(this,"filters.straighten.bgColor")));

		this.add("p",new JLabel(Msg.getString(this,"filters.straighten.mode")));
		this.add(resizeOption);

		this.add("p",showGrid);
	}

	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);

		if (e.getSource()== bgcolor){
			color = bgcolor.getColor();
			straightenFilterPlugin.setColor(color);
			straightenFilterPlugin.renderPreview();
		}else if (e.getSource()== tfAngle){
			try{
				//parse input
				double angle = Double.parseDouble(tfAngle.getText());

				//check values
				angle =(angle%360+360+180)%360-180;

				//round angle to two decimals behind the comma
				tfAngle.setText(Double.toString((int)(angle*100)/(double)100));
				this.angle=angle;
				straightenFilterPlugin.adjustAngle(angle);
			}catch(Exception exception){
				//if anything goes wrong, restore previous value
				tfAngle.setText(Double.toString(angle));
			}
		}else if (e.getSource()== random){
			straightenFilterPlugin.adjustRandom(random.isSelected());
		}else if (e.getSource()== resizeOption){
			if(resizeOption.getSelectedIndex()==0){
				straightenFilterPlugin.setResizeOption(StraightenFilterPlugin.RESIZE_CROPPING);
			}else if(resizeOption.getSelectedIndex()==1){
				straightenFilterPlugin.setResizeOption(StraightenFilterPlugin.RESIZE_KEEP_SIZE);
			}else if(resizeOption.getSelectedIndex()==2){
				straightenFilterPlugin.setResizeOption(StraightenFilterPlugin.RESIZE_ENLARGE);
			}
			straightenFilterPlugin.renderPreview();
		}else if(e.getSource()== showGrid){
			straightenFilterPlugin.setShowGrid(showGrid.isSelected());
			straightenFilterPlugin.renderPreview();
		}

	}


	public void updateAngle(double angle){
		//limit angle so that it displayed between -180 and 180
		this.angle =(angle%360+360+180)%360-180;

		tfAngle.setText(Double.toString((int)(this.angle*100)/(double)100));
	}

	public void setResizeOption(int resizeOption){
		if (resizeOption==StraightenFilterPlugin.RESIZE_KEEP_SIZE){
			this.resizeOption.setSelectedIndex(0);
		}else if (resizeOption==StraightenFilterPlugin.RESIZE_CROPPING){
			this.resizeOption.setSelectedIndex(1);
		}else if (resizeOption==StraightenFilterPlugin.RESIZE_ENLARGE){
			this.resizeOption.setSelectedIndex(2);
		}
	}
	
	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public double getAngle() {
		return angle;
	}

	public void setAngle(double angle) {
		this.angle = angle;
	}
}
